Skip to content

fix(server): preserve task and session ownership during cancellation - #161

Merged
ScriptedAlchemy merged 2 commits into
mainfrom
fix/deep-lifecycle-audit-2026-11-18
Nov 19, 2026
Merged

ScriptedAlchemy merged 2 commits into
mainfrom
fix/deep-lifecycle-audit-2026-11-18

Conversation

@ScriptedAlchemy

@ScriptedAlchemy ScriptedAlchemy commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Cancellation and stale provider callbacks can leave running work untracked or close the session that currently owns it. A deeper audit after #159 reproduced five defects on the merged source:

  • A retired runtime's failed-start cleanup or late terminal event could clear the replacement runtime's busy state when the session, thread, and run identifiers were reused.
  • Native compactThread and resumeTurn bypassed the manager's turn lifecycle, so quiet work could reach the idle close/unload threshold while still active.
  • Cancelling an acknowledged remote launch before its local record was saved left the remote work without a local task or follower.
  • A remote follow-up could be sent before a cancellable metadata read, or lose its local record after acknowledgement. Parent Stop could then miss that work.
  • A settings write could commit its file but skip cache replacement and change publication when cancellation arrived at the rename boundary. A running filesystem watcher can later repair the stale interval, but the immediate committed update was inconsistent.

This change checks raw-runtime ownership before clearing activity and applies the existing turn lifecycle to supported native entrypoints. Remote launches and follow-ups finish local recording and follower admission after an acknowledgement; metadata preflight and remote calls remain interruptible. Same-key retries retain their recorded task identity. Settings publication completes within the existing protected commit section.

Verification

Each defect has a failing regression before its production fix. The final remote follow-up table also proves that cancellation during preflight sends no remote message, checks local ownership before retry, and verifies subsequent Parent Stop behavior.

  • Owning suites: 84 provider-session tests, 53 remote-delegation tests, and 82 settings tests.
  • Additional focused boundaries: 106 adapter/inference cases, 175 forwarding/MCP/group-conversation cases, and 5 access/race cases. These runs use deterministic local services, real SQLite/HTTP where applicable, and protocol fixtures.
  • Independent review covered the provider changes, settings commit boundary, both remote ownership paths, and their combined integration.

On the final combined tree, vp run check:changed 68193a2d1c69d3e237bae0f279228135af1afebe --concurrency 1 passed the native build, formatting, lint, both Knip passes, generated GraphQL checks, and 221 tests across the three owning files plus Agent Context Budget. Its cached typecheck launcher cannot initialize seccomp_unotify in this environment, so the gate invocation exits nonzero. The same configured effect-tsgo tsrs --noEmit checks passed directly for all three affected packages: server, evals, and scripts. No check or budget configuration was weakened.

GitHub Fork CI, including final Fork Check, Agent Context Budget, and every other applicable check must pass on the exact published head before merge.

No live private provider, paid model, native desktop/mobile session, or process-crash guarantee is claimed. The remote fix covers caller cancellation after acknowledgement; transport loss before acknowledgement still relies on the existing idempotent retry contract.

Implemented and verified with GPT-6 Astra Pro / ChatGPT.

Further audit: three additional lifecycle defects

The second pass found and fixed three additional paths on this PR's reviewed head:

  • Delayed remote follow-up after Parent Stop. A follow-up can reach the peer after an earlier Stop has completed, then fail local task recording because the parent is stopped. It now sends a distinct, best-effort compensating Stop. The real peer HTTP/SQLite regression failed before the fix, and cancellation while waiting for that cleanup remains interruptible.
  • Retired native-load completion. An external provider's pending resume can complete after session closure/reopening and mark the replacement's thread as loaded. The replacement then skips its required native resume. Cache completion now verifies the raw runtime for ensure/resume/fork. The existing cache/error regression proves the missing replacement resume before the fix and correct subsequent reuse afterward.
  • Normal Stop with only remote tasks. The client-runtime thread.stop command only queued delegated-task cleanup for local children. It now includes app-owned remote children, including completed remote tasks that can owe a fresh Stop, while preserving the explicit opt-out. A real SQLite regression failed for both running and completed remote-only tasks before the predicate fix; the opt-out control passed. The real consumer preserves completed results and records the owed remote Stop.

All three production changes and their regression boundaries received independent review. Current main, including the already audited #160, is integrated without unrelated source changes. These fixes do not promise remote cleanup when the destination becomes unreachable, and no live private provider or browser execution is claimed.

Latest head verification

Reviewed head: c3aa4694e978561cbc0027c45e07e67c4a80a1ae; its Git tree exactly matches locally validated tree 2d788d54800ced86629d2beb2574eeb6b3210b9e.

vp run check:changed 09efb428b74ef6a1d22be25bdba9c62f8a180669 --concurrency 1 passed all 309 tests across eight files, the native build, formatting, lint, both Knip scans, and generated GraphQL checks. The only failed step was the cached typecheck process launcher (seccomp_unotify is unavailable here). The same configured effect-tsgo tsrs --noEmit checks then passed directly for server, evals, and scripts. No CI or budget gate was changed.

The new head must pass GitHub Fork CI including final Fork Check, Agent Context Budget, and every other applicable check before merge.

Implemented and verified with GPT-6 Astra Pro / ChatGPT.

@changeset-bot

changeset-bot Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: c3aa469

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Stop delayed remote follow-ups whose local task is rejected after Parent Stop. Include remote-only tasks in the normal thread Stop walk. Scope native-load cache completion to its owning raw runtime.

Integrate reviewed main #160. Validated with 309 focused tests, changed-file static gates, and direct server/evals/scripts typechecks.
@ScriptedAlchemy
ScriptedAlchemy merged commit 52f5e3e into main Nov 19, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant